package com.cooud.order;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2025/03/12/19:13
 * @Description:
 */
@EnableFeignClients // 开启Feign客户端
@SpringBootApplication
public class OrderMainApplication {
    public static void main(String[] args) {
        // Spring Boot 启动入口
        SpringApplication.run(OrderMainApplication.class, args);
    }


//    项目启动之后，监听配置文件 配置发生变化时，拿到变化之后的值，
    @Bean
    ApplicationRunner applicationRunner (NacosConfigManager nacosConfigManager) {
        System.out.println("项目启动成功，开始监听配置文件");
        return args -> {
            ConfigService configService = nacosConfigManager.getConfigService(); // 获取配置服务
            configService.addListener("service-order.properties", "DEFAULT_GROUP", new Listener() {
                @Override
                public Executor getExecutor() {
                    return Executors.newFixedThreadPool(4); // 线程池
                }
//                监听到配置变化时，执行该方法 s 代表配置的值
                @Override
                public void receiveConfigInfo(String s) {
                    System.out.println("配置发生变化，新值：" + s);
                }
            });
        };
    }

//    nacos中的配置和当前项目的配置项重复，将以nacos中的配置为准  （外部优先 先导入优先）

}
